<?php
// DB Created by Rich L. a member in service in Northern Delaware.  Please feel free to share this code wherever it may be useful to alcholoics.  Should you have questons please contact me at sobernerd@gmail.com
$jQuery = true;
$tinyMCE = false;
$jQueryCookie = true;
$hoverIntent = true;
$Jededitable = false;
$sectionSort = false;
$googleAPI = false;
$calendar = false;
$deletemeeting = 0;
require_once ('../../config/include_paths.php');
require_once('config.php');
require_once('functions.php');
if($_POST)	{
	$table = $_POST['table'];
	$id = $_POST['id'];
	$returnTo = $_POST['returnTo'];
	if (ifsetor($_GET['deletemeeting'])) {
		$deletemeeting = $_POST['deletemeeting'];
	}
}elseif($_GET) {
	$table = $_GET['table'];
	$id = $_GET['id'];
	$returnTo = $_GET['return_to'];
}
$pageTitle = "Delete data from '.$table.'";
require_once('dbconnect.php');	// database connect script.
session_start();

//Security Check, is a valid user logged in? AND if not then display error message
if ($_SESSION['logged_in'] == 0) {
require ('security_check.php');
// If everything is OK now process the page
} else {
require_once ('adminhead.php');
?>
<body onLoad="checkMenuSettings();">
<div id="wrapper">
  <div id="wrapper">
  <?php require_once ('adminHeader.php'); ?>
  <div class="clearfloat">&nbsp;</div>
  <div id="sidebar">
    <?php require_once ('adminNav.php'); ?>
  </div>
  <div id="main_content">
      <?php 
			// IF the delete button has been pushed
			if ($_POST['submit']){
					// Display Text
					
					$oklink="$table.php";
					$sql = "DELETE FROM $table WHERE id = $id";
					$result = mysql_query($sql) or die(mysql_error());
					
					if ($deletemeeting == 1){
						// Check to see if there are any meetings associated with the group and delete if so
						$sql = "SELECT COUNT(*) FROM meetings WHERE group_id = $id";
						$count_result = mysql_query($sql) or die(mysql_error());
						$num_rows = mysql_num_rows($count_result);
						if($num_rows > 0) {
							$sql = "DELETE FROM meetings WHERE group_id = $id";
							$result = mysql_query($sql) or die(mysql_error());
						}	
					}
					
					echo "<span class='message'>Record Deleted</span>";		
					?>
                    <div>
                      <script language="javascript" type="text/javascript">
						
						$(document).ready(function(){ 
							$('.message').fadeOut(2000, function(){
								window.location = '<?php echo $returnTo ?>'
							});
						});
						</script>
                    </div>
                
		<?php } else {
				// get the id and table variables from the previous page
				
				
				// select record from $tablename
				$sql = "SELECT * FROM $table WHERE id = $id";
				$result = mysql_query($sql) or die(mysql_error());
				
				// IF the form is being requested BEFORE SUBMBMISSION $submit was false then isplay (Are you Sure?....)
				while ($row = mysql_fetch_assoc($result))	{
					switch ($table)
					{
					case "modules":
						echo "<span class='deleteWarning'>Are you sure you want to delete the following MODULE?</span>";
						echo "<br /><span style='margin-left:25px'><div class='module'>$row[editHTML]</div><br />";	
						break;
					case "events":
						echo "<span class='deleteWarning'>Are you sure you want to delete the following EVENT?</span>";
						echo "<br /><span style='margin-left:25px'><div class='module'>$row[editHTML]</div><br />";	
						break;
					case "users":
						echo "<span class='deleteWarning'>Are you sure you want to delete?</span>";
						echo "<br /><span style='margin-left:25px'>$row[fname]</span><br />";
						echo "<span style='margin-left:25px'>$row[lname]<span style='margin-left:25px'><br />";	
						break;
					case "meeting_types":
						echo "<span class='deleteWarning'>Are you sure you want to delete?</span>";
						echo "<br /><span style='margin-left:25px'>$row[code]</span><br />";
						echo "<span style='margin-left:25px'>$row[description]</span><br />";	
						break;
						
					case "intergroup_reps":
						echo "<span class='deleteWarning'>Are you sure you want to delete?</span>";
						echo "<br /><span style='margin-left:25px'>$row[fname]</span><br />";
						echo "<span style='margin-left:25px'>$row[lname]<span style='margin-left:25px'><br />";	
						boldred("Note: This will only delete the person and not the Group or Meetings");
						break;
					
					case "locations":
						echo "<span class='deleteWarning'>Are you sure you want to delete the following location?</span>";
						echo "<br /><span style='margin-left:25px'>$row[name]</span><br />";
						echo "<span style='margin-left:25px'>$row[address]<span style='margin-left:25px'><br />";
						if($address2) {	
						echo "<span style='margin-left:25px'>$row[address2]<span style='margin-left:25px'><br />";
						}
						echo "<span style='margin-left:25px'>$row[city], $row[state] $row[zip]<span><br />";
						$deletemeeting=1;
						$meetings_sql = "SELECT meetings.day as day, meetings.mtime as mtime, groups.groupname as groupname FROM meetings,groups WHERE meetings.group_id = groups.id AND meetings.location_id = $row[id] ORDER BY day";
									$meetings_result = mysql_query($meetings_sql) or die(mysql_error());
									$num_rows = mysql_num_rows($meetings_result);
						if ($num_rows > 0){			
									echo "<br /><span class='deleteWarning'>This will also delete the following meetings!</span>";
												while ($meetings_row = mysql_fetch_assoc($meetings_result))
												{
									// Set $longday
														switch ($meetings_row['day'])
														{
															case 1:
																$longday="Sunday";
																break;
															case 2:
																$longday="Monday";
																break;
															case 3:
																$longday="Tuesday";
																break;
															case 4:
																$longday="Wednesday";
																break;
															case 5:
																$longday="Thursday";
																break;
															case 6:
																$longday="Friday";
																break;
															case 7:
																$longday="Saturday";
																break;
														}	
									// set readable time
											$groupname = $meetings_row['groupname'];
											$meetings_time=$meetings_row['mtime'];
											$rawtime = strtotime($meetings_time);	
											$meetings_time = date('g:iA',$rawtime);							
									echo "<br /><span style='margin-left:25px'>$groupname on $longday at $meetings_time";
												}
											}
						break;
					
					case "groups":
						echo "<span class='deleteWarning'>Are you sure you want to delete?</span>";
						echo "<br /><span style='margin-left:25px'>The $row[groupname] group</span><br />";
						$deletemeeting=1;
						$meetings_sql = "SELECT * FROM meetings WHERE group_id = $row[id] ORDER BY day";
									$meetings_result = mysql_query($meetings_sql) or die(mysql_error());
									$num_rows = mysql_num_rows($meetings_result);
						if ($num_rows > 0){			
									echo "<br /><span class='deleteWarning'>This will also delete the following meetings!</span>";
												while ($meetings_row = mysql_fetch_assoc($meetings_result))
												{
									// Set $longday
														switch ($meetings_row['day'])
														{
															case 1:
																$longday="Sunday";
																break;
															case 2:
																$longday="Monday";
																break;
															case 3:
																$longday="Tuesday";
																break;
															case 4:
																$longday="Wednesday";
																break;
															case 5:
																$longday="Thursday";
																break;
															case 6:
																$longday="Friday";
																break;
															case 7:
																$longday="Saturday";
																break;
														}	
									// set readable time
											$meetings_time=$meetings_row['mtime'];
											$rawtime = strtotime($meetings_time);	
											$meetings_time = date('g:iA',$rawtime);							
									echo "<br /><span style='margin-left:25px'>$longday - $meetings_time";
												}
											}
						break;	
						
					case "meetings":
								// select meeting day
									$meetings_sql = "SELECT * FROM meetings WHERE id = $id";
									
									$meetings_result = mysql_query($meetings_sql) or die(mysql_error());
									
									while ($meetings_row = mysql_fetch_assoc($meetings_result))
									{
									//set group_id
									$group_id = $meetings_row['group_id'];
											// Set $longday
											switch ($meetings_row['day'])
											{
												case 1:
													$longday="Sunday";
													break;
												case 2:
													$longday="Monday";
													break;
												case 3:
													$longday="Tuesday";
													break;
												case 4:
													$longday="Wednesday";
													break;
												case 5:
													$longday="Thursday";
													break;
												case 6:
													$longday="Friday";
													break;
												case 7:
													$longday="Saturday";
													break;
											}
									}		
								// Set Time variables for reading
								$meetings_time=$row[mtime];
								$rawtime = strtotime($meetings_time);	
								$meetings_time = date('g:iA',$rawtime);
								// select groupname
								$groups_sql = "SELECT groupname FROM groups WHERE id = $group_id";
								$groups_result = mysql_query($groups_sql) or die(mysql_error());
								
								while ($groups_row = mysql_fetch_assoc($groups_result)){
									$groupname = $groups_row['groupname'];
								}
						echo "<span class='deleteWarning'>Are you sure you want to delete this meeting?</span>";
						echo "<br /><span style='margin-left:25px'>$longday</span><br />";
						echo "<span style='margin-left:25px'>$meetings_time<span style='margin-left:25px'><br />";	
						echo "<span style='margin-left:25px'>for the $groupname group?</span><br />";	
						break;
						
						default:
						echo "default was picked";
						}								
												}
						?>
      <form id="deleteform" action="<?php echo SITE_ROOT.'public_html/admin/deleteform.php' ?>" method="post" name="deleteform">
        <div>
          <input type="hidden" name="id" value="<?php echo $id ?>" />
          <input type="hidden" name="deletemeeting" value="<?php echo $deletemeeting ?>" />
          <input type="hidden" name="table" value="<?php echo $table ?>" />
          <input type="hidden" name="returnTo" value="<?php echo $returnTo ?>" />
          
          <table class="form_table">
            <tr>
              <td><div align="center">
                  <button name="submit" value="submit" type="submit" class="groovybutton">Yes!</button>
                </div></td>
              <td><div align="center">
                  <input type="button" value="Cancel" class="groovybutton" onClick="history.back()">
                </div></td>
            </tr>
          </table>
        </div>
      </form>
    </div>
  </div>
</div>
</body>
<?php }}
// always disconect
mysql_close($conn);
?>
</html>
